﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace Handrise.CDCable.Mobile.UI
{
    public static class FormManage
    {
        private static FrmBase currentForm = null;
        private static FrmBase privForm = null;

        public static Form firstForm = null;

       // private static Timer formTimer = new Timer();

        private static List<FrmBase> formList = new List<FrmBase>();

        private static Dictionary<string, object[]> allParameter = new Dictionary<string, object[]>();
        
        private static Dictionary<string, object[]> allCache = new Dictionary<string, object[]>(); //缓存的状态

        public static string AppName = "成都电缆巡检系统";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityName"></param>
        /// <returns></returns>
        private static FrmBase GetForm(string formName)
        {
            try
            {
                Assembly assembly = Assembly.GetCallingAssembly();
                return (FrmBase)assembly.CreateInstance("Handrise.CDCable.Mobile.UI." + formName);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        /// <summary>
        /// 进入下一个窗体
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="parameter"></param>
        /// <param name="caches"></param>
        /// <param name="isReturn"></param>
        public static void GotoNext(string formName, object[] parameter, object[] caches, bool isReturn)
        {
            privForm = currentForm;

            //关闭前保存当前窗体的状态
            if (caches != null)
            {

                if (allCache.ContainsKey(currentForm.Name))
                    allCache[currentForm.Name] = caches;
                else
                    allCache.Add(currentForm.Name, caches);
            }


            if (formName != "FrmLogin")
            {
                FrmBase form = GetForm(formName);
                form.Deactivate += form_Deactivate;
                form.Activated += form_Activated;

                if (form == null)
                    return;

                //获取上一个窗体传递过来的参数
                if (parameter != null)
                {
                    form.Parameter = new List<object>(parameter);
                    //保存参数
                    if (allParameter.ContainsKey(formName))
                        allParameter[formName] = parameter;
                    else
                        allParameter.Add(formName, parameter);
                }
                

                //设置是否是返回进入
                form.IsReturn = isReturn;
                
                //设置上一个窗体的名称
                if (privForm != null)
                    form.PriorForm = privForm.Name;

                //如果是返回进入改窗体，则获取缓存的原始状态
                if (allCache.ContainsKey(formName) && isReturn)
                    form.Caches = new List<object>(allCache[formName]);

                //如果是返回进入改窗体，则获取改窗体的父窗体传递过来的参数
                if (allParameter.ContainsKey(formName) && isReturn)
                    form.Parameter = new List<object>(allParameter[formName]);

                //接触上一个窗体的激活和非激活事件关联
                if (privForm != null)
                {
                    privForm.Deactivate -= form_Deactivate;
                    privForm.Activated -= form_Activated;
                }

                //设置当前窗体
                currentForm = form;
                currentForm.Show();

                //关闭上一个窗体
                if (privForm != null)
                {
                    privForm.Close();
                    privForm.Dispose();
                    try
                    {
                        GC.Collect();
                    }
                    catch 
                    {
                    }

                }

            }
            else
            {
                //登录窗体进入如下操作
                if (privForm != null)
                {
                    privForm.Deactivate -= form_Deactivate;
                    privForm.Activated -= form_Activated;
                    privForm.Close();
                    privForm.Dispose();
                    privForm = null;
                }
                firstForm.Text = AppName;
                firstForm.Show();
                firstForm.Activate();
            }
        }


        public static void GotoNext(string formName, List<object> parameter, bool isReturn)
        {

        }


        public static void form_Activated(object sender, EventArgs e)
        {
            if (currentForm != null)
                MakeFullScreen.makeItFullScr(currentForm);
        }

        public static void form_Deactivate(object sender, EventArgs e)
        {
            if (currentForm != null)
                MakeFullScreen.toRegularScr(currentForm);
        }



    }
}
